Imports System.Windows.Forms

Public Class frmStartup

    Private Sub frmStartup_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Text = String.Format("Welcome to WRDB {0}", My.Application.Info.Version.ToString(2))
            AddProjects()
            MainWRDB.CloseSplashScreen()
        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

    Private Sub lstProjects_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstProjects.DoubleClick
        btnProject.PerformClick()
    End Sub

    Private Sub lstTables_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstTables.DoubleClick
        btnTable.PerformClick()
    End Sub

    Private Sub lstProjects_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstProjects.SelectedIndexChanged
        AddTables()
    End Sub

    Private Sub AddProjects()
        With lstProjects.Items
            .Clear()
            Dim ar() As String = MainForm.mruMenuProj.GetFiles()
            If ar IsNot Nothing Then
                For Each s As String In ar
                    If IniFile.GetProjectNames.Contains(s) Then lstProjects.Items.Add(s)
                Next
            End If
            If .Count > 0 Then lstProjects.SelectedIndex = 0
        End With
    End Sub

    Private Sub AddTables()
        Try
            With lstTables.Items
                .Clear()
                MainForm.mruMenuTable.LoadFromRegistry(MRUREGISTRY & "\" & lstProjects.Text)
                .AddRange(MainForm.mruMenuTable.GetFiles)
                If .Count > 0 Then lstTables.SelectedIndex = 0
            End With
        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

    Private Sub btnProject_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnProject.Click
        Try
            Enabled = False
            If lstProjects.SelectedIndex = -1 Then
                MainForm.FileOpenProject()
            Else
                SetProject(lstProjects.Text)
            End If
            MainForm.AddMruProject()
            Close()
        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

    Private Sub btnTable_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnTable.Click
        Try
            Enabled = False
            SetProject(lstProjects.Text)
            MainForm.AddMruProject()
            If lstTables.SelectedIndex = -1 Then
                MainForm.FileOpenWorking()
            Else
                If MainForm.OpenDataTable(lstTables.SelectedItem) Then
                    MainForm.AddMruTable()
                Else
                    Enabled = True
                    MainForm.mruMenuTable.RemoveFile(lstTables.SelectedItem)
                    MainForm.mruMenuTable.SaveToRegistry(MRUREGISTRY & "\" & lstProjects.Text)
                    AddTables()
                    Exit Sub
                End If
            End If
            MainForm.AddMruTable()
            Close()
        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

    Private Sub btnHelp_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHelp.Click
        MainForm.HelpContents()
    End Sub

    Private Sub btnExplore_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExplore.Click
        Try
            If Not SupervisorEnabled() Then Exit Sub

            Enabled = False
            If lstProjects.SelectedIndex = -1 Then
                MainForm.FileOpenProject()
            Else
                SetProject(lstProjects.Text)
            End If
            Visible = False
            MainForm.FileMgmtExplore()
            Close()
        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

    Private Sub OpenTemp()
        Try
            Hide()
            SetProject(lstProjects.Text)
            If MainForm.OpenDataTable(lstTables.SelectedItem) Then
                MainForm.AddMruTable()
            Else
                MainForm.mruMenuTable.RemoveFile(lstTables.SelectedItem)
                MainForm.mruMenuTable.SaveToRegistry(MRUREGISTRY & "\" & lstProjects.Text)
                AddTables()
                Exit Sub
            End If
            Do
                Threading.Thread.Sleep(50)
                Application.DoEvents()
            Loop Until ActiveBrowser.FillIsComplete
        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

    Private Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click
        Try
            Enabled = False
            OpenTemp()
            Dim exp As Boolean = MainWRDB.ExpertMode
            MainWRDB.ExpertMode = True
            MainForm.SelectAll()
            MainForm.OutputExport(lstTables.SelectedItem)
            MainWRDB.ExpertMode = exp
            Close()
        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

    Private Sub btnImport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnImport.Click
        Try
            Enabled = False
            OpenTemp()
            MainForm.EditImportFile()
            Close()
        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

    Private Sub btnManage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnManage.Click
        Try
            Enabled = False
            Close()
            Application.DoEvents()
            MainForm.FileOpenProject()
        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

    Private Sub lnkClear_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkClear.LinkClicked
        With MainForm
            With .mruMenuProj
                .RemoveAll()
                .ClearRegistry()
            End With
            With .mruMenuTable
                .RemoveAll()
                .ClearRegistry()
            End With
        End With
        lstProjects.Items.Clear()
        lstTables.Items.Clear()
    End Sub
End Class
